package com.mdoya.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdoya.domain.Employee;
import com.mdoya.query.EmployeeQuery;
import com.mdoya.response.ResultResponse;
import com.mdoya.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    private final static Logger logger = LoggerFactory.getLogger(EmployeeController.class);
    // 自动注入
/*    @Autowired
    private EmployeeService employeeService;*/

    // 构造器注入
    private EmployeeService employeeService;

    public EmployeeController(EmployeeService empService){
        this.employeeService = empService;
    }

    /**
     * 员工登录
     * @param employee
     * @return
     */
//    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @PostMapping("/login")
    public ResultResponse login(@RequestBody Employee employee, HttpSession session) {
        log.info("员工登录: {}",employee);
        Employee emp = employeeService.login(employee);
        if (emp != null) {
            session.setAttribute("employee", emp);
            return ResultResponse.success(emp);
        }
        return ResultResponse.error("用户名和密码错误！");
//        return emp != null ? ResultResponse.success(emp) : ResultResponse.error("用户名和密码错误！");
    }

    /**
     *  退出登录
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public ResultResponse logout(HttpSession session){
        session.removeAttribute("employee");
        return session.getAttribute("employee") == null ? ResultResponse.success("退出成功") : ResultResponse.error("退出失败");
    }

    /**
     *      查询所有员工
     * @param employeeQuery 当前页数量
     * @return
     */
    @GetMapping("/page")
    public ResultResponse selectList(EmployeeQuery employeeQuery){
        log.info("查询所有员工: {}",employeeQuery);
        IPage<Employee> list = employeeService.selectPage(employeeQuery);
        if (list != null){
            return ResultResponse.success(list);
        }
        return ResultResponse.error("查询所有员工失败");
    }
/*    @GetMapping("/{id}")
    public ResultResponse selectById(@PathVariable String id){
        log.info("根据id查询: {}",id);
        Employee employee = employeeService.selectById(id);
        if (employee != null){
            return ResultResponse.success(employee);
        }
        return ResultResponse.error("查询员工失败");
    }*/

    /**
     *      添加员工
     * @param employee 员工信息
     * @param session 请求session
     * @return
     */
    @PostMapping
    public ResultResponse insert(@RequestBody Employee employee, HttpSession session){
        Employee emp = (Employee)session.getAttribute("employee");
        log.info("添加员工操作: {}",emp);
        Integer rows = employeeService.insert(employee,emp.getId());
        if (rows > 0){
            return ResultResponse.success("添加员工成功");
        }
        return ResultResponse.error("添加员工失败");
    }
    /**
     *      修改员工
     * @param employee 员工信息
     * @param session 请求session
     * @return
     */
    @PutMapping
    public ResultResponse updateById(@RequestBody Employee employee, HttpSession session){
        Employee emp = (Employee)session.getAttribute("employee");
        log.info("记录操作修改操作员工: {}",emp);
        Integer rows = employeeService.updateById(employee,emp.getId());
        if (rows > 0){
            return ResultResponse.success("修改员工成功");
        }
        return ResultResponse.error("修改员工失败");
    }
}
